package su.FeatureSelection;

import iitb.Model.FeatureIdentifier;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import su.Ner.U;

public class FeatureSelection {

	FeatureStore featureStore;

	public FeatureSelection(FeatureStore _featureStore) {
		featureStore=_featureStore;
	}

	public Hashtable featureSelection() {
		HashSet<String> usefulFeatures = cpFeatureSelection(1);
		usefulFeatures=remove0Feature(usefulFeatures);
		return modifyFeatureId(usefulFeatures);
	}

	HashSet<String> rFeatureSelection() {

		HashSet<String> res = new HashSet<String>();

		Iterator it = featureStore.featurePrecision.entrySet().iterator();
		int i = 0;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			i++;
			if (i % 2 == 0)
				continue;
			res.add(entry.getKey().toString());
		}
		return res;
	}
	
	HashSet<String> deleteOneOccurrenceFeatureSelection() {

		HashSet<String> res = new HashSet<String>();

		Iterator it = featureStore.featurePrecision.entrySet().iterator();

		for(String f:featureStore.featureCount.keySet()){
			if(featureStore.featureCount.get(f)>1)
				res.add(f);
		}
		return res;
	}

	static // modify id of each feature
	Hashtable modifyFeatureId(HashSet<String> usefulFeatures) {

		Hashtable res = new Hashtable();
		int i = 0;
		for (String feature : usefulFeatures) {

			FeatureIdentifier fi=new FeatureIdentifier(feature);
			//fi.id=i;
			if(res.get(fi)!=null)
				System.out.println();
			
			if(fi.toString().equals("PW_:-11506:0"))
				System.out.println("hahs");
			
			res.put(fi, i);
			i++;
			//System.out.println(features.get(feature));
		}
		if(usefulFeatures.size()!=res.size()){
			System.out.println("usefulFeatures.size()!=res.size()");
			System.exit(-1);
		}
		return res;
	}

	public HashSet<String> cpFeatureSelection(double acceptRatio) {
		
		ArrayList<KeyValuePair> list = new ArrayList<KeyValuePair>();

		Iterator itX = featureStore.predicateCount.entrySet().iterator();

		while (itX.hasNext()) {

			Map.Entry entry = (Map.Entry) itX.next();
			String x = entry.getKey().toString();

			Iterator itY = featureStore.YCount.entrySet().iterator();
			while (itY.hasNext()) {
				Map.Entry entry1 = (Map.Entry) itY.next();
				String label = entry1.getKey().toString();

				String feature = x + ":" + label;
				feature=featureStore.featureWithId.get(feature);
				if (null != feature){
					list.add(new KeyValuePair(feature,featureStore.featurePrecision.get(feature)));
					featureStore.featurePrecision.put(feature,featureStore.featurePrecision.get(feature));
				}
			}
		}
		
		U.sort(list);
		
		HashSet<String> res = new HashSet<String>();
		for (int i = 0; i < acceptRatio * list.size(); i++) {
			res.add(list.get(i).key);
		}

		return res;
	}

	static HashSet<String> miFeatureSelection() {
		return null;
	}

	static HashSet<String> ksFeatureSelection() {
		return null;
	}

	public static Hashtable featureSelectionFromLambda(Hashtable stringToInt, double[] lambda,int minusHowMany){
		ArrayList<KeyValuePair> list = new ArrayList<KeyValuePair>();
		Iterator it = stringToInt.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			double value=lambda[(Integer)stringToInt.get(key)];
			list.add(new KeyValuePair(key.toString(),   Math.abs(value)));
		}
		U.sort(list);
		
		HashSet<String> featureSet=new HashSet<String>();
		for(int i=0;i<list.size()-minusHowMany;i++){
			featureSet.add(list.get(i).key);
		}
		return modifyFeatureId(featureSet);
	}
	
	public HashSet<String> remove0Feature(HashSet<String> usefulFeatures){
		HashSet<String> res=new HashSet<String>();
		for(String f:usefulFeatures){
			if(f.charAt(f.length()-1)=='0'&&(f.substring(0,2).equals("PW")||f.substring(0,2).equals("NW") ))
				continue;
			res.add(f);
		}
		return res;
	}
}
